/**
 * 
 */
package com.alstudio.event;


/**
 * SubscriptionObject封装了事件订阅对象。内含注册到事件管理器的订阅类以及订阅类所实现的订阅回调
 * @author alonso lee
 *
 */
public class SubscriptionObject {
	//事件订阅对象
    final Object subscriber;
    //事件订阅对象实现的回调方法
    final SubscriberCallback subscriberCallback;
    //回调对象优先级。值越大，事件投递的优先级越高
    final int priority;
    /**
     * Becomes false as soon as {@link EventBus#unregister(Object)} is called, which is checked by queued event delivery
     * {@link EventBus#invokeSubscriber(PendingPost)} to prevent race conditions.
     */
    volatile boolean active;

    /**
     * 创建事件订阅对象
     * @param subscriber			注册到事件管理器的订阅类
     * @param subscriberCallback	事件订阅类实现的订阅回调
     * @param priority				事件优先级		
     */
    SubscriptionObject(Object subscriber, SubscriberCallback subscriberCallback, int priority) {
        this.subscriber = subscriber;
        this.subscriberCallback = subscriberCallback;
        this.priority = priority;
        active = true;
    }

    @Override
    public boolean equals(Object other) {
        if (other instanceof SubscriptionObject) {
            SubscriptionObject otherSubscriptionObject = (SubscriptionObject) other;
            return subscriber == otherSubscriptionObject.subscriber
                    && subscriberCallback.equals(otherSubscriptionObject.subscriberCallback);
        } else {
            return false;
        }
    }

    @Override
    public int hashCode() {
        return subscriber.hashCode() + subscriberCallback.methodString.hashCode();
    }

}
